// $Id: ScreenList.java 12 2009-01-04 00:03:18Z moyekj@yahoo.com $

package com.hme.tivo.videostream;

import com.tivo.hme.bananas.BEvent;
import com.tivo.hme.bananas.BList;
import com.tivo.hme.bananas.BView;
import com.tivo.hme.bananas.BText;

class ScreenList extends BList {   
   public ScreenList(BView parent, int x, int y,
      int width, int height, int rowHeight) {
      super(parent, x, y, width, height, rowHeight);
      setBarAndArrows(BAR_HANG, BAR_DEFAULT, null, "push");
   }
    
   protected void createRow(BView parent, int index) {
      debug.print("parent=" + parent + " index=" + index);
      String name = get(index).toString();
      String value = new GLOBAL().makeFileName(name);
      VText text = new VText(
         parent, GLOBAL.text_X, GLOBAL.text_Y, 1, "small"
      );
      text.setFlags(RSRC_HALIGN_LEFT);
      text.setValue(value);
      // Add an icon for folder types
      if (name.startsWith(GLOBAL.FOLDER_PREFIX)) {
         // Icon resource for folder types
         BView icon = new BView(
            parent, GLOBAL.icon_X, GLOBAL.icon_Y,
            GLOBAL.icon_W, text.h, true
         );
         icon.setResource(GLOBAL.icon_PICTURE);
      }
   }

   // Clear out all entries & resources from BList
   public void clear() {
      debug.print("");      
      for (int i=0; i<this.size(); i++) {
         BView p = this.getRow(i);
         // Remove BView row children
         for (int j=0; j<p.getChildCount(); j++) {
            if ( p.getChild(j).getClass().getSimpleName().equals("BText") ) {
               // Clear text resource since BText remove doesn't do so!!
               BText t = (BText)p.getChild(j);
               t.setValue(null);
            }
            p.getChild(j).clearResource();
            p.getChild(j).remove();
         }
         // Remove BView and ViewScreen
         p.clearResource();
         p.remove();
         ViewScreen v = (ViewScreen)get(i);
         v.remove();
      }
      super.clear();
   }
   
   public boolean handleFocus(boolean focus, BView v1, BView v2) {
      debug.print("focus=" + focus + " v1=" + v1 + " v2=" + v2);
      if (v1 == null) {
         return false;
      }
      return super.handleFocus(focus, v1, v2);
   }
       
   public boolean handleKeyPress(int code, long rawcode) {
      debug.print("code=" + code + " rawcode=" + rawcode);
      switch (code) {
         case KEY_SELECT:
            postEvent(new BEvent.Action(this, "push"));
            return true;
         case KEY_ADVANCE: // Jump to bottom of list or from bottom to top
            if (this.getFocus() == this.size()-1) {
               this.setFocus(0,true);
               play("pageup.snd");
            } else {
               this.setFocus(this.size()-1,true);
               play("pagedown.snd");
            }
            return true;
         case KEY_PLAY:
            GLOBAL.Resume = true;
            postEvent(new BEvent.Action(this, "push"));
            return true;
         case KEY_ENTER:
            postEvent(new BEvent.Action(this, "enter"));
            return true;
      }
      return super.handleKeyPress(code, rawcode);
   }
}
